****************************
***** CLEAN REFERENDA DATA 
****************************

*** LOAD RAW REFERENDA DATA FROM VOTECAL DATABASE
// Prop 66 -- 2004 -- YES DECREASES PENALTIES (weakens three-strikes laws) 
import dbase ../raw/votecal_referenda/statewide_PR_66.dbf,clear
ren PR_66_Y yes
ren PR_66_N no
gen prop66_2004_pct = yes / ( yes + no )
gen prop66_2004_tot = yes + no
gen prop66_2004_yes = yes 
gen elec_yr = 2004
ren Geoid10 cb
keep cb prop* elec_yr
assert length(cb) ==15
gen cbg = substr(cb, 1, 12)
gen ct = substr(cb, 1, 11)
keep if substr(cb,1,5) == "06037" 
// above restricts to LA County, expanding to all does not affect match with main.dta
count
save ../raw/votecal_referenda/prop66_2004.dta, replace

// Prop 5 -- 2008 -- YES DECREASES PENALTIES (more drug diversion, less harsh drug penalties)
import dbase ../raw/votecal_referenda/statewide_PR_5.dbf,clear
ren PR_5_Y yes
ren PR_5_N no
gen prop5_2008_pct = yes / ( yes + no )
gen prop5_2008_tot = yes + no
gen prop5_2008_yes = yes
gen elec_yr = 2008
ren Geoid10 cb
keep cb prop* elec_yr
assert length(cb) ==15
gen cbg = substr(cb, 1, 12)
gen ct = substr(cb, 1, 11)
keep if substr(cb,1,5) == "06037"
count
save ../raw/votecal_referenda/prop5_2008.dta, replace

// Merge raw referenda data together
use ../raw/votecal_referenda/prop66_2004.dta,clear
append using ../raw/votecal_referenda/prop5_2008.dta
gen pro_reform_pct = prop5_2008_pct if elec_yr ==2008
replace pro_reform_pct = prop66_2004_pct if elec_yr ==2004
keep elec_yr pro_reform_pct cb 
save ../temp/referenda.dta,replace

// Identify treatment CBs and their control CBGs &
//		placebo CBs and their placebo control CBGs
use ../data/main.dta,clear
keep if sample_main==1
drop treat*
cap drop placebo
gen shooting = tau==0 
gen shooting_yr = elec_yr if shooting==1
gen treat1 = shooting_yr == 2006 | shooting_yr==2008
bys cb: egen treat = max(treat1) 
gen treat_post = elec_yr==2008 & treat==1
gen placebo1 = shooting_yr == 2010
bys cb: egen placebo = max(placebo1) 
gen placebo_post = placebo==1 & elec_yr==2008
bys CBG (cb elec_yr): egen sample_treat = max(treat) 
bys CBG (cb elec_yr): egen sample_placebo = max(placebo) 
keep if elec_yr == 2004 | elec_yr == 2008
keep CBG cb elec_yr treat treat_post sample_treat placebo placebo_post sample_placebo sample_main
count
save ../temp/referenda_treatment.dta,replace 

*** MERGE REFERENDA WITH STUDY DATA ON SHOOTINGS
use ../data/main.dta,clear
keep cb CBG elec_yr sample_main sample_armed sample_unarmed all_c00 all_c10
keep if elec_yr == 2004 | elec_yr == 2008
// merge referenda data
merge 1:1 elec_yr cb using ../temp/referenda.dta, assert(1 3)
sum all_c10 all_c00 if _merge ==1,det
// almost all CBs without referenda data have small populations
drop _merge
// merge treatment CBs and their CBGs plus placebo CBs and CBGs
merge 1:1 cb elec_yr using ../temp/referenda_treatment.dta, assert(1 3) 
assert sample_main==0 if _merge==1
drop if sample_main==0 
drop _merge
// check that each CBG with a treat/placebo CB also have a treat/placebo CB
foreach g in treat placebo {
	bys CBG (cb elec_yr): egen CBG_max_`g' = max(`g')
	assert CBG_max_`g' ==1 if sample_`g' ==1 
}

*** MAKE VARIABLES FOR ANALYSIS
gen treat_post_armed = treat==1 & elec_yr==2008 & sample_armed_known==1
gen treat_post_unarmed = treat==1 & elec_yr==2008 & sample_unarmed_known==1
gen placebo_post_armed = placebo==1 & elec_yr==2008 & sample_armed_known==1
gen placebo_post_unarmed = placebo==1 & elec_yr==2008 & sample_unarmed_known==1
keep if sample_main==1 & ( sample_treat ==1 | sample_placebo ==1)
save ../data/referenda.dta,replace

*** CONFIRM TABLE OUTPUT
use ../data/referenda.dta,clear
assert sample_main==1

//TREATMENT EFFECTS
sum pro_reform_pct if elec_yr==2004 & treat==1
reghdfe pro_reform_pct treat_post if sample_treat, cluster(CBG) absorb(cb elec_yr) noconstant
reghdfe pro_reform_pct treat_post_armed treat_post_unarmed if sample_treat, cluster(CBG) absorb(cb elec_yr) noconstant

//PLACEBO EFFECTS
sum pro_reform_pct if elec_yr==2004 & placebo==1
reghdfe pro_reform_pct placebo_post if sample_placebo, cluster(CBG) absorb(cb elec_yr) noconstant
reghdfe pro_reform_pct placebo_post_armed placebo_post_unarmed if sample_placebo, cluster(CBG) absorb(cb elec_yr) noconstant



